package cn.hd01.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import cn.hd01.repository.WithdrawBookRepository;
import cn.hd01.repository.entity.WithdrawBook;
import cn.hd01.service.WithdrawBookService;

@Service
public class WithdrawBookServiceImpl extends BaseServiceImpl<WithdrawBook, Integer> implements WithdrawBookService {

	private WithdrawBookRepository repository;

	@Autowired
	public WithdrawBookServiceImpl(WithdrawBookRepository repository) {
		super(repository);
		this.repository = repository;
	}

	@Override
	public Page<WithdrawBook> findByRefIdAndType(Integer refId, String type, Pageable pageable) {
		return repository.findByRefIdAndType(refId, type, pageable);
	}

	@Override
	public Page<WithdrawBook> findAll(final WithdrawBook withdrawBook,
			Pageable pageable) {
		if (withdrawBook == null) {
			return repository.findAll(pageable);
		}

		return repository.findAll(Specifications.where(new Specification<WithdrawBook>() {
			@Override
			public Predicate toPredicate(Root<WithdrawBook> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<Predicate>();
				
				if (StringUtils.hasText(withdrawBook.getType())) {
					predicates.add(cb.equal(r.get("type").as(String.class), withdrawBook.getType()));
				}
				
				if (withdrawBook.getRefId() != null) {
					predicates.add(cb.equal(r.get("refId").as(Integer.class), withdrawBook.getRefId()));
				}
				
				if (StringUtils.hasText(withdrawBook.getStatus()) && !"-1".equals(withdrawBook.getStatus())) {
					predicates.add(cb.equal(r.get("status").as(String.class), withdrawBook.getStatus()));
				}

				if (StringUtils.hasText(withdrawBook.getCustomerName())) {
					predicates.add(cb.like(r.get("customerName").as(String.class), "%" + withdrawBook.getCustomerName() + "%"));
				}
				
				if(!CollectionUtils.isEmpty(withdrawBook.getDistributorIdList())){
					predicates.add(r.get("refId").in(withdrawBook.getDistributorIdList()));
				}
				
				return cb.and(predicates.toArray(new Predicate[] {}));
			}
		}), pageable);
	}

	@Override
	public BigDecimal findWithdrawReabate(String beginDate, String endDate) {
		if(StringUtils.hasText(beginDate) && StringUtils.hasText(endDate)){
			return repository.findWithdrawRebate(beginDate, endDate);
		}else if(StringUtils.hasText(beginDate)){
			return repository.findWithdrawRebateByBegin(beginDate);
		}else if(StringUtils.hasText(endDate)){
			return repository.findWithdrawRebateByEnd(endDate);
		}else {
			return repository.findWithdrawRebate();
		}
	}

}
